added some development tools
[windows-sources.git] / developer / Samples / NET 4.6 / Samples for Parallel / ClassLibrary1 / TaskSchedulers / ThreadPerTaskkScheduler.cs
blob83df527cbfba17d6bb2c8e45f38a1f882f4bed46
1 //--------------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // File: ThreadPerTaskScheduler.cs
6 //
7 //--------------------------------------------------------------------------
9 using System.Collections.Generic;
10 using System.Linq;
12 namespace System.Threading.Tasks.Schedulers
14 /// <summary>Provides a task scheduler that dedicates a thread per task.</summary>
15 public class ThreadPerTaskScheduler : TaskScheduler
17 /// <summary>Gets the tasks currently scheduled to this scheduler.</summary>
18 /// <remarks>This will always return an empty enumerable, as tasks are launched as soon as they're queued.</remarks>
19 protected override IEnumerable<Task> GetScheduledTasks() { return Enumerable.Empty<Task>(); }
21 /// <summary>Starts a new thread to process the provided task.</summary>
22 /// <param name="task">The task to be executed.</param>
23 protected override void QueueTask(Task task)
25 new Thread(() => TryExecuteTask(task)) { IsBackground = true }.Start();
28 /// <summary>Runs the provided task on the current thread.</summary>
29 /// <param name="task">The task to be executed.</param>
30 /// <param name="taskWasPreviouslyQueued">Ignored.</param>
31 /// <returns>Whether the task could be executed on the current thread.</returns>
32 protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
34 return TryExecuteTask(task);